package com.example.truthordare;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Playgame extends Activity {
	int lv;
	int i = 0;
	int type;
	final static ArrayList<Question> questions = new ArrayList<Question>();
	Context context;
	private static final String TAG = Playgame.class.getSimpleName();
	public ListView lvquestion;
	TextView tvmain;
	int round = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_playgame);
		context = this;

		tvmain = (TextView) findViewById(R.id.play_name);
		lvquestion = (ListView) findViewById(R.id.play_listViewQuestion);
		ImageButton imgcancel = (ImageButton) findViewById(R.id.img_play_cancel);
		ImageButton imgok = (ImageButton) findViewById(R.id.img_add1);
		ImageButton imgface = (ImageButton) findViewById(R.id.play_img_face);
		ImageButton imgcamera = (ImageButton) findViewById(R.id.play_img_camera);
		ImageButton imgtime = (ImageButton) findViewById(R.id.play_img_timer);
		ImageButton imgref = (ImageButton) findViewById(R.id.play_img_ref);
		loadnoti();
		Bundle receivebd = this.getIntent().getExtras();
		if (receivebd != null) {
			lv = receivebd.getInt("lv");
			i = receivebd.getInt("i");
			// type = receivebd.getInt("type");
		}
		imgref.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainActivity.tempplayer.get(i).setSroce(
						MainActivity.tempplayer.get(i).getSroce() - 2);
				questions.clear();
				loadquestion();

				QuestionAdapter quesadap = new QuestionAdapter(
						getApplicationContext(), questions);
				lvquestion.setAdapter(quesadap);

			}
		});
		imgface.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});

		imgcancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (i < questions.size()) {
					MainActivity.tempplayer.get(i).setSroce(
							MainActivity.tempplayer.get(i).getSroce() - 5);
					int a = MainActivity.tempplayer.get(i).sroce;
					Intent in = new Intent(getApplicationContext(),
							Notificate.class);
					startActivityForResult(in, 0);
					i++;
				} else {
					Intent mainIntent = new Intent();
					mainIntent.setClass(getApplicationContext(),
							MathScore.class);
					Bundle send = new Bundle();
					send.putInt("round", round);
					mainIntent.putExtras(send);
					startActivityForResult(mainIntent, 2);
					// i = 0;
				}

			}

		});

		imgok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (i < questions.size()) {
					Intent intent = new Intent(getApplicationContext(),
							Rate.class);
					startActivityForResult(intent, 1);
				} else {
					Intent intent = new Intent(getApplicationContext(),
							Rate.class);
					startActivityForResult(intent, 1);
				}

			}
		});

	}

	public void loadquestion() {
		try {
			DBAdapter db = new DBAdapter(this);
			Cursor c = db.selectquestion(type, lv);
			Log.i("loadquestion", " a");
			while (c.moveToNext()) {
				int id = Integer.parseInt(c.getString(c.getColumnIndex("_id")));
				String question = c.getString(c.getColumnIndex("Question"));
				int type = c.getInt(c.getColumnIndex("Type"));
				String lv = c.getString(c.getColumnIndex("LV"));
				Question ques = new Question(id, question, type, lv);
				questions.add(ques);
				Toast.makeText(getApplicationContext(), "" + question,
						Toast.LENGTH_SHORT).show();
				Log.i("load question", "ok " + question);
			}
			Log.i("loadquestion", " b");
			c.close();

		} catch (Exception es) {
			es.printStackTrace();
		} finally {

		}
	}

	public void loadnoti() {
		// Log.i("loadnoti", "loaf ok");
		Intent mainIntent = new Intent();
		Bundle sendBundle = new Bundle();
		sendBundle.putInt("i", i);

		mainIntent.setClass(getApplicationContext(), Notificate.class);
		mainIntent.putExtras(sendBundle);
		startActivityForResult(mainIntent, 0);
		// Log.i("loadnoti", "loaf ok");
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub

		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				Bundle receivebd = data.getExtras();
				if (receivebd != null) {
					type = receivebd.getInt("type", 1);
				}
				questions.clear();
				loadquestion();

				QuestionAdapter quesadap = new QuestionAdapter(
						getApplicationContext(), questions);
				lvquestion.setAdapter(quesadap);

				tvmain.setText(MainActivity.tempplayer.get(i).getPlayer());
				// loadnoti();
			}
		} else if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				Bundle receivebd = data.getExtras();
				if (receivebd != null) {
					int make = receivebd.getInt("make", 3);
					MainActivity.tempplayer.get(i).setSroce(
							MainActivity.tempplayer.get(i).getSroce() + make);
					// int a = MainActivity.tempplayer.get(i).sroce;
					// questions.clear();
					if (i < questions.size()) {
						i++;
						Toast.makeText(this, "i= " + i, Toast.LENGTH_SHORT);
						loadnoti();
					} else {
						Intent mainIntent = new Intent();
						mainIntent.setClass(getApplicationContext(),
								MathScore.class);
						Bundle send = new Bundle();
						send.putInt("round", round);
						mainIntent.putExtras(send);
						startActivityForResult(mainIntent, 2);
						// i = 0;
					}

				}
			}
		} else if (requestCode == 2) {
			if (resultCode == RESULT_OK) {
				String name = "";
				Bundle reciveBundle = data.getExtras();
				if (reciveBundle != null) {
					round = reciveBundle.getInt("round");
					name = reciveBundle.getString("name");
				}
				i = 0;
				if (round == 10) {
					Intent mainIntent = new Intent();
					mainIntent.setClass(getApplicationContext(), Victory.class);
					Bundle send = new Bundle();
					send.putString("name", name);
					mainIntent.putExtras(send);
					startActivityForResult(mainIntent, 3);

				} else {
					loadnoti();
				}

			}
		} else if (requestCode == 3) {
			if (resultCode == RESULT_OK) {
				finish();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.playgame, menu);
		return true;
	}

}
